// APP对象
const app = getApp()
// 工具函数
const util = require('../../utils/util')
// animation动画实例
var animationFn = wx.createAnimation({
  transformOrigin: "0 50%",
  duration: 1000,
  timingFunction: "ease",
  delay: 0
})

Page({
  data: {
    height: [],
    heightCount: 300,
    topNum: 160,
    animationFn: {},
    itemHeight: 10,
    edit: false,
    sex: 0
  },
  // 页面加载
  onLoad: function (options) {
    // 编辑身高
    if (!util.isEmptyObject(options)) {
      var height = this.data.topNum
      var sex = this.data.sex
      // 判断options.height是否为null
      if ((options.height != 'null') && (options.height != 'undefined')){
        height = options.height
      }
      // 判断options.sex是否为null
      if ((options.sex != 'null') && (options.sex != 'undefined')) {
        sex = options.sex
      }
      this.setData({
        topNum: height,
        edit: true,
        sex: sex
      })
    } else {
      // 初始化用户资料设置性别
      this.setData({
        sex: app.updateUserInfo.sex
      })
    }
  },
  // 页面首次渲染完
  onReady: function () {
    this.init()
  },
  // 初始化
  init: function () {
    var height = []
    for (var i = 0; i <= this.data.heightCount; i++) {
      var item = {}
      item.height = i
      item.lineFlag = (item.height % 10 === 0) ? true : false
      height.push(item)
    }
    this.setData({
      height: height
    })
    this.setHeight()
  },
  // 监听触屏开始
  touchStart: function (e) {
    var pageY = e.touches[0].pageY

    this.scrollFlag = true
    this.start = {
      distance: this.data.topNum * this.data.itemHeight,
      y: pageY
    }
  },
  // 监听触屏滚动
  touchMove: function (e) {
    var pageY = e.touches[0].pageY
    var distance = pageY - this.start.y
    var top

    if (!this.scrollFlag) {
      return
    }

    this.start.y = pageY
    this.start.distance = this.start.distance - distance

    animationFn.translateY(-this.start.distance).step({ duration: 120 })
    this.setData({
      animationFn: animationFn.export()
    })

    top = parseInt(this.start.distance / this.data.itemHeight)
    if (top < 0) {
      top = 0
    } else if (top > this.data.heightCount) {
      top = this.data.heightCount
    }
    this.setData({
      topNum: top
    })
  },
  // 监听触屏结束
  touchEnd: function () {
    this.scrollFlag = false
    var distance = this.start.distance
    var top

    if (distance < 0) {
      distance = 0
    } else if (distance > this.data.heightCount * this.data.itemHeight) {
      distance = this.data.heightCount*this.data.itemHeight
    } else { }

    top = parseInt(distance / this.data.itemHeight)
    this.setData({
      topNum: top
    })

    this.setHeight()
  },
  // 设置滚动距离
  setHeight: function () {
    var top = this.data.topNum * this.data.itemHeight
    animationFn.translateY(-top).step()
    this.setData({
      animationFn: animationFn.export()
    })
  },
  tapGoBack: function () {
    wx.navigateBack()
  },
  tapGoNext: function () {
    var height = this.data.height[this.data.topNum]['height']
    // 编辑身高
    if(this.data.edit){
      // 获取前一页数据
      var userInfo = util.getPrevPageData('userInfo')
      userInfo.stature = height
      // 修改前一页数据
      util.editPrevPageData({
        userInfo: userInfo
      })
      // 设置本页数据
      this.setData({
        height: height
      })
      wx.navigateBack()
    } else {
      // 初始化用户资料选择身高
      app.updateUserInfo.height = height
      wx.navigateTo({
        url: '/pages/loginStep4/loginStep4',
      })
    }
  }
})